package com.example.testoink;

import java.util.Arrays;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import com.example.testoink.R.layout;



import android.R.id;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class MainActivity extends Activity {

	
	Pig pigArray[];
	
	RelativeLayout myLayout;

	int timeLeft;
	//TextView txt1 ;
	int pigCount = 0;
	int heartLeft = 10;
	int money = 1000;
	int currentWave = 0;
	int maxWave = 5;
	TextView coinInfo;
	TextView heartInfo;
	TextView waveInfo;
	int currentEnemyCount = 0;
	
	int perWave = 1;
	int currentEnemeAtWave = 0;
	
	Boolean haveGrass[];
	int slotStatus[];
	Enemy enemyArray[];
	
	Boolean gameEnd = false;
	
	Timer  waveTimer;
	
	int stageNum;
	
	public void adjustStage(){
		
		if(stageNum==1){
			
			money = 1000;
			
			maxWave = 5;
			
			perWave = 1;
			
			
		}
		
if(stageNum==2){
			
			money = 500;
			
			maxWave = 7;
			
			perWave = 1;
			
			
		}

if(stageNum==3){
	
	money = 250;
	
	maxWave = 10;
	
	perWave = 2;
	
	
}
		
		
		
	}
	
	
	public void goHome(View v){
		
	Intent intent = new Intent(MainActivity.this,SelectStageActivity.class);
		
		startActivity(intent);
		
	}
	
	public void restart(View v){
		
		Intent intent = new Intent(MainActivity.this,MainActivity.class);
		
		startActivity(intent);
		
	}
	
	public void endGame(){
		
		gameEnd = true;
		
		for(int i = 0 ; i<10 ; i++){
			
			if(enemyArray[i]!=null){
				
				enemyArray[i].goodbye();
				
			}
			
		}
		
		Intent intent = new Intent(MainActivity.this,Result.class);
		
		intent.putExtra("Result", "Lose");
		
		startActivity(intent);
		
		
	}
	
public void wonGame(){
		
		gameEnd = true;
		waveTimer.cancel();
		
		for(int i = 0; i< pigCount ; i ++ ){
			
			//pigArray[i].endAll();
			
			
		}
		
		for(int i = 0 ; i<10 ; i++){
			
			if(enemyArray[i]!=null){
				
				enemyArray[i].goodbye();
				
			}
			
		}
		
		
		Intent intent = new Intent(MainActivity.this,Result.class);
		
		intent.putExtra("Result", "Won");
		
		startActivity(intent);
		
		
		
	}
	
	public void setText(String txt){
		
		
		//txt1.setText(txt);
		
	}
	
	public Enemy[] getEnemyArray(){
		return enemyArray;		
		
	}
	
	public void addEnemy(){
		
		if(currentEnemyCount<10){
		
		Enemy newEnemy = new Enemy(myLayout, this, currentEnemyCount);
		
		newEnemy.drawOnLayout(currentEnemyCount,100,205);
		
		int emptySlot;
		
		for(int i = 0; i<10 ; i++){
			
			
			if(enemyArray[i]==null){
				
				emptySlot = i;
				
				enemyArray[emptySlot] = newEnemy;
				
				currentEnemeAtWave++;
				currentEnemyCount++;
				
				System.out.println("asdsd"+i);
				break;
			}
			
		}
		
	 }
	}
	
	public Boolean eatGrassAt(Pig pigTarget,int at){
		
		Boolean tmp = haveGrass[at];
		
		if(tmp){
			
			myLayout.removeView(findViewById(100+at));
			haveGrass[at] = false;
			
			if(slotStatus[at] == 1){
				
				pigTarget.tranfrom();
			}
			
			slotStatus[at] = -1;
		}
		
		return tmp;
		
	}
	
public void buyFlameGrass(View v){
		
		if(money>=100){
			
			int grassSlot = 0;
			
			for(int i = 0;i<8;i++){
				
				if(haveGrass[i]==false){
					
					grassSlot = i;
					haveGrass[i] = true;
					slotStatus[i] = 1;
					break;
					
				}
				
			}
			
		
		//myLayout = (RelativeLayout)findViewById(R.id.rela1);
		updateCoinBy(-100);
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(1920/12,1028/6);
		ImageView grass = new ImageView(this);
		grass.setImageResource( R.drawable.grass2 );
		grass.setX(335+(375*((grassSlot)%4)));
		grass.setY(105+(300*((grassSlot)/4)));
		grass.setId(100+grassSlot);
		myLayout.addView(grass, params);
		
		//grass drop
		
		 	TranslateAnimation tAnimation = new TranslateAnimation(0,0,0,100);
	        tAnimation.setDuration(500);
	        tAnimation.setFillAfter(true);
	        //tAnimation.setRepeatCount(0);
	       // tAnimation.setRepeatMode(Animation.REVERSE);
	       // grass.setAnimation(tAnimation);
		    grass.startAnimation(tAnimation);
		    
	        
		}
		//newPig.drawOnLayout(20+pigCount,120+(168*((pigCount-1)%4)),40+(210*((pigCount-1)/4)));
		
	}
	
	public void buyGrass(View v){
		
		if(money>=10){
			
			int grassSlot = 0;
			
			for(int i = 0;i<8;i++){
				
				if(haveGrass[i]==false){
					
					grassSlot = i;
					haveGrass[i] = true;
					slotStatus[i] = 0;
					break;
					
				}
				
			}

		//myLayout = (RelativeLayout)findViewById(R.id.rela1);
		updateCoinBy(-10);
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(1920/12,1028/6);
		ImageView grass = new ImageView(this);
		grass.setImageResource( R.drawable.grass );
		grass.setX(335+(375*((grassSlot)%4)));
		grass.setY(105+(300*((grassSlot)/4)));
		grass.setId(100+grassSlot);
		myLayout.addView(grass, params);
		
		
		//grass drop
		
		 	TranslateAnimation tAnimation = new TranslateAnimation(0,0,0,100);
	        tAnimation.setDuration(500);
	        tAnimation.setFillAfter(true);
	        //tAnimation.setRepeatCount(0);
	       // tAnimation.setRepeatMode(Animation.REVERSE);
	       // grass.setAnimation(tAnimation);
		    grass.startAnimation(tAnimation);
		    
	        
		}
		//newPig.drawOnLayout(20+pigCount,120+(168*((pigCount-1)%4)),40+(210*((pigCount-1)/4)));
		
	}
	
	public void addCoinInfo(){
		
		coinInfo = new TextView(this);
		
		coinInfo.setText(money+"");
		
		coinInfo.setX(290);
		
		coinInfo.setY(-8);
		
		myLayout.addView(coinInfo);
	}
	
	public void updateCoin(){
		
		coinInfo.setText(money+"");
		
	}
	
public void updateCoinBy(int by){
		
	    money += by;
	
	    updateCoin();
	    
		//coinInfo.setText(money+"");
		
	}
	
public void addHeartInfo(){
		
	heartInfo = new TextView(this);
		
	heartInfo.setText(heartLeft+"/10");
		
	heartInfo.setX(100);
		
	heartInfo.setY(-8);
		
	myLayout.addView(heartInfo);
	
	}

public void updateHeart(){
	
	heartInfo.setText(heartLeft+"/10");
	
}

public void updateHeartBy(int by){
	
	heartLeft += by;
	
	if(heartLeft<=0){
		
		if(!gameEnd){
			
			endGame();
			
		}
		
		heartLeft = 0;}
	heartInfo.setText(heartLeft+"/10");
	
}

public void removeEnemy(Enemy target){
	
	target.goodbye();
	
	for(int i =0; i<10;i++){
		
		if(enemyArray[i]!=null&&enemyArray[i]==target){
			
			//enemyArray[i].goodbye();
			
			enemyArray[i] = null;
			
			break;
			
		}
		
	}

	
	//currentEnemyCount--;
	
}

public void updateWave(){
	
	waveInfo.setText(currentWave+"/"+maxWave);
	
}

public void addWaveInfo(){
	
	waveInfo = new TextView(this);
		
	waveInfo.setText(currentWave+"/"+maxWave);
		
	waveInfo.setX(120);
		
	waveInfo.setY(50);
		
	myLayout.addView(waveInfo);
	
	}


	public void addPig(View v){
		
		if(money>=100){
			
			money-=100;
			
			updateCoin();
       /* RelativeLayout myLayout = (RelativeLayout)findViewById(R.id.rela1);
		
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100,100);*/
		pigCount++;
		
		if(pigCount<=8){
			
			Pig newPig = new Pig(myLayout,this,1);
			
			newPig.drawOnLayout(20+pigCount,305+(380*((pigCount-1)%4)),110+(450*((pigCount-1)/4)));
			
			
			/*for(int j = 0; j<10 ; j++){
				
				int emptySlot = 0;
				
				if(pigArray[j]==null){
					
					emptySlot = j;
					
					//pigArray[emptySlot] =newPig;
		
					break;
				}
				
			}*/
			}
			
		}

	}
	
public void waveStart(){
		
		waveTimer = new Timer();	

		waveTimer.scheduleAtFixedRate(new TimerTask() {
	
			public void run() {
	            runOnUiThread(new Runnable() //run on ui thread
	             {
	              public void run() 
	              {
	            		//money+=50;
	            	  
	            	  if(currentEnemeAtWave<perWave){
	            		addEnemy();
	            		
	            		//currentEnemeAtWave++;
	            		
	            		if(currentEnemeAtWave==perWave){
	            			

	            			currentEnemeAtWave = 0;
	            			
	            			currentWave++;
	            			
	            			updateWave();
	            			
	            			waveTimer.cancel();
	            			
	            			waveStart();
	            			
	            			
	            			if(currentWave>=maxWave){
	            				
	            				wonGame();
	            				
	            			}
	            			
	            		}
	            	  }
	            		//updateCoin();
	              }
	             });
	        }
	    
			},15000,15000);
		
		
	}
	
	/*public void pigStartToDropCoin(){
		
		final Timer  pigTimer = new Timer();	

		pigTimer.scheduleAtFixedRate(new TimerTask() {
	
			public void run() {
	            runOnUiThread(new Runnable() //run on ui thread
	             {
	            	
	            
	              public void run() 
	              {
	            		money+=50;
	            		
	            		updateCoin();
	              }
	             });
	        }
	    
			},5000,5000);
		
		
	}*/

	public void deTime(){
		
		 // TextView txt1 = (TextView)findViewById(R.id.textView1);	
		  //txt1.setText(" ");
		
		
	}
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
		setContentView(R.layout.activity_main);
		myLayout = (RelativeLayout)findViewById(R.id.rela1);
		
		stageNum = getIntent().getIntExtra("stageNum",stageNum);
  
		adjustStage();
		
		/*final Timer  t2 = new Timer();	
		txt1 = (TextView)findViewById(R.id.textView1);	
		 timeLeft = 120;
		
		txt1.setText("");
		
		t2.scheduleAtFixedRate(new TimerTask() {
	
			public void run() {
	            runOnUiThread(new Runnable() //run on ui thread
	             {
	            	
	            
	              public void run() 
	              {
	            		timeLeft--;
	            		
	            	  if(timeLeft<0){timeLeft = 0;
	            	  t2.cancel();}
	            	 
	        		 // txt1.setText(timeLeft+"");
	              }
	             });
	        }
	    
			},new Date(),1000);*/
		
	
		addCoinInfo();
		addHeartInfo();
		addWaveInfo();
		
		haveGrass =  new Boolean[8];
		slotStatus = new int[8];
		Arrays.fill(haveGrass, Boolean.FALSE);
		Arrays.fill(slotStatus,-1);
		enemyArray = new Enemy[10];
		//pigStartToDropCoin();
		
		waveStart();
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public Enemy[] getEnemyArray1() {
		// TODO Auto-generated method stub
		return enemyArray;
	}

}
